#共享单车骑行预测
import pandas as pd
import numpy as np
from sklearn.metrics import r2_score
import matplotlib.pyplot as plt

train = pd.read_csv("FE_day.csv")
print(train.head())
#train.info()

# 分离输入特征x和输出y
y = train["cnt"]
X = train.drop('cnt', axis = 1)
feat_names = X.columns

from sklearn.model_selection import train_test_split
# 20%的数据构建测试样本
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=40, test_size=0.2)
print(X_train.shape)

# #最小二乘线性回归
from sklearn.linear_model import LinearRegression
# 1.使用默认配置初始化学习器实例
lr = LinearRegression()
# 2.用训练数据训练模型参数
lr.fit(X_train, y_train)
# 3. 用训练好的模型对测试集进行预测
y_test_pred_lr = lr.predict(X_test)
y_train_pred_lr = lr.predict(X_train)

# fs = pd.DataFrame({"columns":list(feat_names), "coef":list((lr.coef_.T))}) #feat_names权重系数对应的特征  特征  系数
# fs.sort_values(by=['coef'],ascending=False) #系数值降序排列
# #print(fs)


#岭回归／L2正则   交叉验证
from sklearn.linear_model import RidgeCV
#1.  r*范围
alphas = [ 0.01, 0.1, 1, 10,100]
#2. 生成一个RidgeCV实例
ridge = RidgeCV(alphas=alphas, store_cv_values=True)
#3. 模型训练
ridge.fit(X_train, y_train)
#4. 预测
y_test_pred_ridge = ridge.predict(X_test)
y_train_pred_ridge = ridge.predict(X_train)
# 评估，使用r2_score评价模型在测试集和训练集上的性能
print('The r2 score of RidgeCV on test is', r2_score(y_test, y_test_pred_ridge))
print('The r2 score of RidgeCV on train is', r2_score(y_train, y_train_pred_ridge))

#正则化的线性回归（L1正则 --> Lasso）
from sklearn.linear_model import LassoCV
#2. 生成学习器实例
#lasso = LassoCV(alphas=alphas)
#1. 设置超参数搜索范围

#2 生成LassoCV实例（默认超参数搜索范围）
lasso = LassoCV()
#3. 训练（内含CV--交叉验证）
lasso.fit(X_train, y_train)
#4. 测试
y_test_pred_lasso = lasso.predict(X_test)
y_train_pred_lasso = lasso.predict(X_train)
# 评估，使用r2_score评价模型在测试集和训练集上的性能
print('The r2 score of LassoCV on test is', r2_score(y_test, y_test_pred_lasso))
print('The r2 score of LassoCV on train is', r2_score(y_train, y_train_pred_lasso))

mses = np.mean(lasso.mse_path_, axis=1)
plt.plot(np.log10(lasso.alphas_), mses)
plt.xlabel('log(alpha)')
plt.ylabel('mse')
# plt.show()
print('alpha is:', lasso.alpha_)  # r变化是MSE曲线
# 各特征的权重系数，系数的绝对值大小可视为该特征的重要性
fs = pd.DataFrame({"columns":list(feat_names), "coef_lr":list((lr.coef_.T)), "coef_ridge":list((ridge.coef_.T)), "coef_lasso":list((lasso.coef_.T))})
fs.sort_values(by=['coef_lr'],ascending=False)
print(fs)

#在训练集上观察预测残差的分布，看是否符合模型假设：噪声为0均值的高斯噪声(噪声符合高斯分布)
f, ax = plt.subplots(figsize=(7, 5))
f.tight_layout()
ax.hist(y_train - y_train_pred_lr, bins=40, label='Residuals Linear', color='b', alpha=.5)
ax.set_title("Histogram of Residuals")
ax.legend(loc='best')
plt.show()

'''  小结
1）综合岭回归得分最高，系数收缩，说明加入正则对系数有一定的影响，在一定程度上可以防止过拟合
2）最小二乘看出系数占比大的如温度，季节3，体感温度，与实际相符
3）Lasso得分最低，看到大多系数为零
4） 岭回归测试比训练R2score得分低的多
'''